package com.qqbot.impl.task;

import com.alibaba.fastjson.JSONObject;
import com.qqbot.core.core.Bot;
import com.qqbot.core.core.BotFactory;
import com.qqbot.core.core.Member;
import com.qqbot.core.message.MessageChain;
import com.qqbot.impl.utils.HttpClientUtils;
import com.qqbot.impl.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

@Component
public class RobotTaskTalkAt {

    @Autowired
    RedisUtils redisUtils;

    private static final long groupId = 478728208;

    @Scheduled(cron = "05 0 8,22 * * ?")
    //@Scheduled(cron = "0 0/2 * * * ?")
    public void waterOne() throws Exception {
        List<Bot> botList = BotFactory.getBots();
        if (CollectionUtils.isEmpty(botList)) {
            return;
        }
        Bot bot = botList.get(0);
        Collection<Member> list = bot.getMembers(groupId);
        List<Long> needAtList = new ArrayList<>();
        for (Member member: list) {
            String key = groupId + "" + member.getUserId();
            if(!redisUtils.hasKey(key)){
                continue;
            }
            long time = Long.valueOf(redisUtils.get(key));
            long timeNow = System.currentTimeMillis();
            int days = (int) ((timeNow - time) / (1000*3600*24));
            if(days > 7){
                needAtList.add(member.getUserId());
            }
        }
        MessageChain mc = new MessageChain();
        if(!CollectionUtils.isEmpty(needAtList)){
            for (Long userId: needAtList) {
                mc.at(userId);
            }
            mc.text("你们再不说话，群主就把你们踢了！！！");
            bot.sendGroupMessage(groupId, mc);
        }

    }

    @Scheduled(cron = "0/30 * * * * ?")
    public void outGroup() throws Exception {
        List<Bot> botList = BotFactory.getBots();
        if (CollectionUtils.isEmpty(botList)) {
            return;
        }
        Bot bot = botList.get(0);
        Collection<Member> list = bot.getMembers(groupId);
        Set<String> keys = redisUtils.getKeys(groupId + "*");
        for (String s : keys) {
            if(!list.stream().filter(item -> (groupId + "" + item.getUserId()).equals(s)).findAny().isPresent()){
                MessageChain mc = new MessageChain();
                redisUtils.delete(s);
                String qq = s.substring(9);
                String rsJson = HttpClientUtils.get("https://ovooa.com/API/qqxx/?QQ=" + qq, 5000,5000);
                JSONObject jsonObject = JSONObject.parseObject(rsJson);
                JSONObject dataObject = jsonObject.getJSONObject("data");
                String name = dataObject.getString("name");
                String img = dataObject.getString("imgurl");
                mc.text("QQ：" + qq + "\n");
                mc.text("网名：" + name + "\n");
                mc.image(img);
                mc.text("我检测到这个B退出了群，点名批评，大家注意！");
                bot.sendGroupMessage(groupId, mc);
            }
        }
    }

}
